package xyz.ieden.hello.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.ieden.commons.entity.User;
import xyz.ieden.hello.service.IUserService;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(value = "user")
public class UserController {

    private static transient final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private Optional<IUserService> userService;

    @GetMapping(value = "")
    public List<User> getUser() {
        if (userService.isPresent()) {
            List<User> userList = userService.get().getUserList();
            LOGGER.info(userList.toString());
            return userList;
        } else {
            return null;
        }
    }

    @GetMapping(value = "{id}")
    public User getUserById(@PathVariable(value = "id") Integer id) {
        if (userService.isPresent()) {
           User user = userService.get().getUserById(id);
            LOGGER.info(user.toString());
            return user;
        } else {
            return null;
        }
    }
}